package com.crl.rcdp.sys.controller;

import com.cars.util.global.GlobalReturnCode;
import com.cars.util.json.JsonResult;
import com.cars.util.json.JsonResultUtil;
import com.cars.util.validation.DeleteValidGroup;
import com.crl.rcdp.sys.dto.SysAppDTO;
import com.crl.rcdp.sys.service.SysAppService;
import com.crl.rcdp.sys.vo.SysAppSearchVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @author: 孔垂云
 * @version: 1.0
 * @date: 2020/6/22 17:43
 */
@RestController
@RequestMapping("/sys/app")
@Api(tags = "应用管理")
public class SysAppController {

    @Autowired
    private SysAppService sysAppService;

    /**
     * 获取所有应用信息
     */
    @PostMapping("/list")
    @ApiOperation(value = "应用列表")
    public JsonResult list(@RequestBody SysAppSearchVO sysAppSearchVO) {
        return JsonResultUtil.ok(sysAppService.page(sysAppSearchVO));
    }


    /**
     * 新增应用
     *
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增应用")
    public JsonResult add(@RequestBody SysAppDTO sysAppDTO) {
        int flag = sysAppService.add(sysAppDTO);
        if (flag == 1) {
            return JsonResultUtil.ok();
        } else {
            return JsonResultUtil.error(GlobalReturnCode.SERVER_ERROR);
        }
    }

    /**
     * 修改应用
     *
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改应用")
    public JsonResult update(@RequestBody SysAppDTO sysAppDTO) {
        int flag = sysAppService.update(sysAppDTO);
        if (flag == 1) {
            return JsonResultUtil.ok();
        } else {
            return JsonResultUtil.error(GlobalReturnCode.SERVER_ERROR);
        }
    }

    /**
     * 删除角色
     *
     * @return
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除角色")
    public JsonResult delete(@Validated({DeleteValidGroup.class}) @RequestBody SysAppDTO sysAppDTO) {
        int flag = sysAppService.delete(sysAppDTO.getAppid());
        if (flag == 1) {
            return JsonResultUtil.ok();
        } else {
            return JsonResultUtil.error(GlobalReturnCode.SERVER_ERROR);
        }
    }


    /**
     * 获取所有角色信息
     */
    @PostMapping("/get")
    @ApiOperation(value = "获取单个用户")
    public JsonResult get(@RequestBody SysAppSearchVO sysAppSearchVO) {
        SysAppDTO sysAppDTO = sysAppService.get(sysAppSearchVO.getAppid());
        return JsonResultUtil.ok(sysAppDTO);
    }

    /**
     * 获取所有角色信息
     */
    @PostMapping("/viewAppsecrect")
    @ApiOperation(value = "查看appsecrect")
    public JsonResult viewAppsecrect(@RequestBody SysAppSearchVO sysAppSearchVO) {
       String appsecrect = sysAppService.getAppsecrect(sysAppSearchVO.getAppid());
        return JsonResultUtil.ok(appsecrect);
    }

}
